'use strict'
const Controller  = require('egg').Controller

class MainController extends Controller{
    async index(){
        this.ctx.body='api'
    }
    async checkLogin(){
        let userName = this.ctx.request.body.userName
        let password = this.ctx.request.body.password
        const sql = "select userName from admin_user where userName = '" +
                userName+"'and password = '"+password+"'"
        const res = await this.app.mysql.query(sql)
        if(res.length>0){
            let openId = new Date().getTime()
            this.ctx.session.openId ={'openId':openId}
            this.ctx.body = {'date':'登陆成功','openId':openId}
        }else{
            this.ctx.body = {'date':'登陆失败'}
        }
    }
    async getTypeInfo(){
        const resType = await this.app.mysql.select('type')
        this.ctx.body = {data:resType}
    }
    async addArticle(){
        let tmpArticle = this.ctx.request.body
        console.log(tmpArticle)
        const result = await this.app.mysql.insert('article',tmpArticle)
        const insertSuccess = result.affectedRows === 1
        const insertId = result.insertId
        this.ctx.body = {
            isSuccess:insertSuccess,
            insertId:insertId,
        }
    }
    async updateArticle(){
        let tmpArticle = this.ctx.request.body
        console.log(tmpArticle)
        const result = await this.app.mysql.update('article',tmpArticle)
        const updateSuccess = result.affectedRows === 1
        console.log(updateSuccess)
        this.ctx.body = {
            isSuccess:updateSuccess,
        }
    }
    async updateLeaveMessageWhether(){
        let id = this.ctx.params.id
        const result = await this.app.mysql.update('leaveMessage',{whether:1},{where:{'id':id}})
        const updateSuccess = result.affectedRows === 1
        this.ctx.body = {
            isSuccess:updateSuccess,
        }
    }
    async getArticleList(){
        let sql = 'select article.id as id, '+
        'article.title as title,'+
        'article.introduce as introduce, '+
        "article.addTime as addTime, "+
        'article.view_count as view_count, '+
        'article.content as content, '+
        'type.typeName as typeName '+
        'from article left join type on article.type_id = type.typeNum '+
        'order by article.id desc'
        const result = await this.app.mysql.query(sql)
        this.ctx.body = {list:result}
    }
    async getLeaveMessageList(){
        let id = this.ctx.params.id
        let sql = 'select articleId as articleId, '+
        'id as id,'+
        'name as leaveName,'+
        'message as leaveMessage, '+
        "time as time, "+
        'whether as whether '+
        'from leaveMessage '+
        'order by id desc'
        const result = await this.app.mysql.query(sql)
        this.ctx.body = {list:result}
    }
    async deleteArticle(){
        // console.log(ctx)
        let id = this.ctx.params.id
        const res = await this.app.mysql.delete('article',{'id':id})
        this.ctx.body = {
            data:res
        }
    }
    async deleteLeaveMessage(){
        // console.log(ctx)
        let id = this.ctx.params.id
        const res = await this.app.mysql.delete('leaveMessage',{'id':id})
        this.ctx.body = {
            data:res
        }
    }
    async getArticleById(){
        let id = this.ctx.params.id
        let sql = 'select article.id as id, '+
        'article.title as title,'+
        'article.introduce as introduce, '+
        "article.addTime as addTime, "+
        'article.content as content, '+
        'type.typeName as typeName, '+
        'article.type_id as typeId '+
        'from article left join type on article.type_id = type.typeNum '+
        'where article.id=' +id
        const result = await this.app.mysql.query(sql)
        this.ctx.body = {data:result}
      }
}
module.exports = MainController;